<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="指派负责人" prop="appointUserName">
            <el-input v-model="formItem.appointUserName" placeholder="请选择指派人" @focus="handleUserIconClick" />
          </el-form-item>
        </el-col>
        <el-col :offset="20" :span="4">
          <el-button size="mini" type="primary" @click="ok('submit')">指派</el-button>
        </el-col>
      </el-row>
    </el-form>
    <SelectDialog v-if="userSelDialogFormVisible" title="选择负责人" :append-to-body="true" :show="userSelDialogFormVisible" @on-ok="selectUserOk" @on-cancel="selectUserCancel">
      <UsersPage ref="usersTable" :for-selection="true" :multiple-selected="true" :org-code="formItem.orgCode" />
    </SelectDialog>
  </div>
</template>

<script>
import { detail,
  create
} from '@neu/dcom-ui/api/incidentAppoint'
import SelectDialog from '@neu/biz-core/components/SelectDialog'
import UsersPage from '@neu/auth-ui/views/users/tables'
const formItem = {
  name: '',
  objCode: null,
  objName: '',
  userCode: '',
  userName: '',
  applyTypeName: '',
  applyType: '',
  communicationNumber: '',
  applyTime: '',
  applyTimeShow: '',
  coverageInfluence: '',
  urgency: '',
  priority: '',
  incidentFrom: '',
  description: '',
  appointUserCode: '',
  appointUserName: '',
  executeUserCode: '',
  executeUserName: '',
  workNum: '',
  taskId: ''
}
export default {
  name: 'IncidentAppointForm',
  components: {
    UsersPage,
    SelectDialog
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      /** 是否显示用户选择对话框 */
      userSelDialogFormVisible: false,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        appointUserName: [{
          required: true,
          message: '指派人不能为空',
          trigger: 'change'
        }]
      }
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    ok(value) {
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            this.formItem.workOrderNum = this.workOrderNum
            this.formItem.taskId = this.taskId
            create(this.formItem).then(res => {
              if (res.status === 'success') {
                this.$emit('cancelNewOrderForm', 'refresh')
              }
            })
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    /** 选择用户按钮 */
    handleUserIconClick() {
      this.userSelDialogFormVisible = true
    },
    selectUserOk() {
      var users = this.$refs.usersTable.selections
      if (users.length > 1) {
        return this.$message({
          showClose: true,
          message: '只能选择一个负责人',
          type: 'error'
        })
      }
      if (users.length < 1) {
        this.formItem.appointUserCode = ''
        this.formItem.appointUserName = ''
      } else {
        this.formItem.appointUserName = users[0].name ? users[0].name : users[0].username
        this.formItem.appointUserCode = users[0].username
      }
      this.userSelDialogFormVisible = false
    },
    selectUserCancel() {
      this.userSelDialogFormVisible = false
    }
  }
}
</script>
